package com.leoblog.test.dao;

import java.util.Set;

import com.leoblog.dao.impl.CategoryDao;
import com.leoblog.dao.impl.EntryDao;
import com.leoblog.dao.impl.UserDao;
import com.leoblog.dao.impl.WebSiteDao;
import com.leoblog.domain.Category;
import com.leoblog.domain.Entry;
import com.leoblog.domain.Tag;
import com.leoblog.domain.User;
import com.leoblog.domain.WebSite;
import com.leoblog.test.CategoryMother;
import com.leoblog.test.EntryMother;
import com.leoblog.test.TagMother;
import com.leoblog.test.UserMother;
import com.leoblog.test.WebSiteMother;
import com.leoblog.test.base.AbstractUTestBaseSpringDaoTestCase;

public class UTestEntryDao extends AbstractUTestBaseSpringDaoTestCase{
	
	private EntryDao entryDao;

	private UserDao userDao;
	
	private WebSiteDao webSiteDao;
	
	private CategoryDao categoryDao;
	
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public WebSiteDao getWebSiteDao() {
		return webSiteDao;
	}

	public void setWebSiteDao(WebSiteDao webSiteDao) {
		this.webSiteDao = webSiteDao;
	}

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public EntryDao getEntryDao() {
		return entryDao;
	}

	public void setEntryDao(EntryDao entryDao) {
		this.entryDao = entryDao;
	}
	
	
	public void testAddEntry() throws Exception{
				
		Entry entry = this.createEntry();
		
		Category expectedCategory = entry.getCategory();
		User expectedAuthor = entry.getAuthor();
		WebSite expectedWebSite = entry.getWebSite();
		
		entryDao.save(entry);
		
		assertNotNull(entry.getId());
		assertEquals(expectedCategory, entry.getCategory());
		assertEquals(expectedAuthor, entry.getAuthor());
		assertEquals(expectedWebSite, entry.getWebSite());
		
		Entry loadedEntry = entryDao.get(entry.getId());
		assertNotNull(loadedEntry);
		Set<Tag> tags = loadedEntry.getTags();
		
		Tag tagInSet = tags.iterator().next();
		
		assertNotNull(tags);
		assertTrue(tags.size() == 1);
		assertNotNull(tagInSet.getId());
		assertEquals("testTagName", tagInSet.getName());
		
		
		assertEquals(entry, loadedEntry);
	}
	
	public void testUpdateEntry() throws Exception{
		
		Entry entry = this.createEntry();
				
		// add into database
		entryDao.save(entry);
		
		//load title
		Entry loadedEntry = entryDao.get(entry.getId());
		
		//update it
		loadedEntry.setTitle("updatedTitle");
		
		//update tag
		loadedEntry.getTags().clear();
		Tag tag2 = TagMother.newInstance();
		tag2.setName("UpdatedTagName");
		loadedEntry.getTags().add(tag2);
		
		//persist it
		entryDao.save(loadedEntry);
		
		//load it again
		Entry loadAgainEntry = entryDao.get(entry.getId());
		
		assertNotNull(loadAgainEntry);
		assertNotNull(loadAgainEntry.getId());
		assertEquals("updatedTitle", loadAgainEntry.getTitle());
		
		Set<Tag> tags = loadAgainEntry.getTags();
		Tag loadedTag2 = tags.iterator().next();
		
		assertTrue(tags.size() == 1);
		assertEquals("UpdatedTagName", loadedTag2.getName());
	}
	/**
	 * 
	 * createEntry, Create on Aug 27, 2008
	 * @author<a href="mailto:nicholas85211@gmail.com">Nicholas</a>
	 * @return Entry
	 */
	private Entry createEntry(){
		User author = UserMother.newInstance();
		
		Category category = CategoryMother.newInstance();
		
		WebSite webSite = WebSiteMother.newInstance();
		
		Tag tag = TagMother.newInstance();
		
		webSite.setUser(author);
		
		userDao.save(author);

		categoryDao.save(category);
		
		webSiteDao.save(webSite);
		
		assertNotNull(author);
		
		assertNotNull(category);
		
		assertNotNull(webSite);
		
		Entry entry = EntryMother.newInstance();
		entry.setAuthor(author);
		entry.setCategory(category);
		entry.setWebSite(webSite);
		entry.getTags().add(tag);
		return entry;
	}

}
